package com.skwen.ssm.controller;

import com.skwen.ssm.po.MyResponseEntity;
import com.skwen.ssm.po.User;
import com.skwen.ssm.service.UserService;
import com.skwen.ssm.util.Code;
import com.skwen.ssm.util.OidGen;
import com.skwen.ssm.util.ToolUitls;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.swing.text.html.parser.Entity;
import javax.xml.ws.Response;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class ViewController {

    private Logger logger = Logger.getLogger(ViewController.class);

    @Resource(name = "UserService")
    private UserService userService;


    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public MyResponseEntity register(@RequestBody User user) {
        MyResponseEntity myResponseEntity = new MyResponseEntity();
        if (user != null) {
            user.setId(OidGen.getInstance().getId());

            if (ToolUitls.isEmpty(user.getUserAccount()) && !ToolUitls.isEmpty(user.getUserPhone())) {
                user.setUserAccount(user.getUserPhone());
            }

            if (ToolUitls.isEmpty(user.getUserPhone())) {
                myResponseEntity.setCode(Code.failed);
                myResponseEntity.setMsg("手机号为空");
            } else {
                int count = userService.ifUserRegister(user.getUserPhone());
                if (count > 0) {
                    myResponseEntity.setCode(Code.failed);
                    myResponseEntity.setMsg("您的手机号已注册过");
                } else {
                    userService.register(user);
                    myResponseEntity.setCode(Code.success);
                    myResponseEntity.setBody(user);
                    myResponseEntity.setMsg("注册成功");
                }
            }
        }
        return myResponseEntity;
    }


    @ResponseBody
    @RequestMapping(value = "/getListUser", method = RequestMethod.GET)
    public Map<String, Object> getListUser() {
        Map<String, Object> map = new HashedMap();
        List<User> list = userService.getListUser();
        map.put("list", list);
        return map;
    }


//    @ResponseBody
//    @RequestMapping(value = "/ifUserRegister",method = RequestMethod.GET)
//    public int ifUserRegister(@Param("userPhone") String userPhone){
//        return userService.ifUserRegister(userPhone);
//    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public MyResponseEntity login(@Param("userAccount") String userAccount, @Param("userPsd") String userPsd) {
        MyResponseEntity myResponseEntity = new MyResponseEntity();
        User user = userService.login(userAccount, userPsd);
        if (user != null) {
            myResponseEntity.setCode(Code.success);
            myResponseEntity.setBody(user);
            myResponseEntity.setMsg("登录成功");
        } else {
            myResponseEntity.setCode(Code.failed);
            myResponseEntity.setMsg("登录失败，请检查您的账号密码是否有误。");
        }
        return myResponseEntity;
    }

}